<template>
  <div class="panelsbox">
    <cube-scroll class="leftpanels">
      <ul>
        <li v-for="(item,index) of tabList"
            :key="index"
            :class="item.active? 'active' : '' "
            @click="selectList(index)">
          {{item.label}}</li>
      </ul>
    </cube-scroll>
    <cube-scroll class="rightpanels">
      <ul>
        <li v-for="(tag,index) of imgs"
            :key="index">
          <img :src="tag.image"
               alt="">
          <p>{{tag.label}}<i class="cubeic-add"
               @click="addToCart($event, tag)"></i></p>
        </li>
      </ul>
    </cube-scroll>
    <div class="ball-warp">
      <transition @before-enter="beforeEnter"
                  @enter="enter"
                  @afterEnter="afterEnter">
        <div class="ball"
             v-if="ball.show">
          <div class="inner">
            <i class="cubeic-add"></i>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BotnavList',
  data () {
    return {
      ball: {
        show: false,
        el: ''
      },
      imgs: [],
      tabList: [
        {
          label: '热门推荐',
          active: true
        },
        {
          label: '手机数码',
          active: false
        },
        {
          label: '家用电器',
          active: false
        },
        {
          label: '电脑办公',
          active: false
        },
        {
          label: '美妆护肤',
          active: false
        },
        {
          label: '口红',
          active: false
        },
        {
          label: '手袋',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        },
        {
          label: '情趣记生',
          active: false
        }
      ]
    }
  },
  //获取分类
  methods: {
    selectList (index) {
      this.tabList.forEach((val, ind) => {
        if (index == ind) {
          val.active = true
        } else {
          val.active = false
        }
      })
      this.getClassIfy(index)
    },
    //axios获取数据
    async getClassIfy (index) {
      const result = await this.$http.get('/api/classify', { params: { type: index } })
      this.imgs = result.data
    },
    //添加到购物车
    addToCart (e, tag) {
      this.$store.commit('addCart', tag)
      //让小球显示
      this.ball.show = true
      //获取点击元素
      this.ball.el = e.target
    },
    beforeEnter (el) {
      //让小球移动到点击的位置
      console.log(el)
      const dom = this.ball.el
      const rect = dom.getBoundingClientRect()
      console.log(rect.left)
      console.log(window.innerWidth * 0.7)
      const x = rect.left - window.innerWidth * 0.7
      const y = -(window.innerHeight - rect.top)
      console.log(x, y)
      el.style.display = 'block'
      el.style.transform = `translate3d(0,${y}px,0)`
      const inner = el.querySelector('.inner')
      inner.style.transform = `translate3d(${x}px,0,0)`
      inner.style.opacity = '1'
    },
    enter (el, done) {
      document.body.offsetHeight
      const inne = el.querySelector('.inner')
      el.style.transform = `translate3d(0,0,0)`
      inne.style.opacity = '0.9'
      inne.style.transform = `translate3d(0,0,0)`
      el.addEventListener('transitionend', done)
    },
    afterEnter (el) {
      this.ball.show = false
      el.style.display = 'none'
    }
  },
  //获取第一次请求数据
  created () {
    this.getClassIfy(0)
  },
  mounted () {
    //设置滚动盒子高度
    const leftpanels = document.querySelector('.leftpanels')
    const rightpanels = document.querySelector('.rightpanels')
    const bodyHeight = document.documentElement.clientHeight
    leftpanels.style.height = bodyHeight - 57 + 'px'
    rightpanels.style.height = bodyHeight - 57 + 'px'
  }
}
</script>

<style lang="stylus" scoped>
.panelsbox
  display: flex
  .leftpanels
    width: 30%
    li
      height: 50px
      line-height: 50px
      border-bottom: 1px solid #ffffff
      color: #333333
      background: #f8f8f8
      font-size: 14px
    .active
      background: #ffffff
      color: #e93b3d
  .rightpanels
    width: 70%
    ul
      display: flex
      flex-wrap: wrap
      li
        width: 50%
        justify-content: center
        align-items: center
        font-size: 15px
        img
          width: 80px
          height: 80px
        .cubeic-add
          font-size: 18px
          margin-left: 8px
  .ball-wrap, .ball
    position: fixed
    left: 70%
    bottom: 10px
    z-index: 10011
    color: red
    transition: all 1s cubic-bezier(0.49, -0.29, 0.75, 0.41)
    .inner
      width: 16px
      height: 16px
      transition: all 1s linear
</style>
  